import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Form, NumberBox, Popup, SelectBox, TextBox, } from "devextreme-react";
import React, { useCallback, useImperativeHandle, useRef, } from "react";
import { useState } from "react";
import { SimpleItem } from "devextreme-react/form";
import { CpasIcon } from "~/public/cpas-ui";
import ReportPopup from "../../ReportPopup";
import AddPopup from "./components/AddPopup";
import notify from "devextreme/ui/notify";
import PopupComponents from "../PopupComponents";
import { getSubjectInfo } from "./service";
import PTextBox from "../PTextBox";
import "./_style.less";
const SubjectPopupData = React.forwardRef((props, ref) => {
    const { AccountDataID, dbname, username } = props;
    const diaParams = useRef();
    const SubjectRef = useRef();
    const statementProjectRef = useRef();
    const PopupComponentsRef = useRef();
    const pTextBoxRef = useRef();
    const PTextBoxValue = useRef();
    const [visible, setVisible] = useState(false);
    const [selectedYears, setSelectedYears] = useState("");
    // const [SubjectNumber, setSubjectNumber] = useState<string>('');
    const [SelectType, setSelectType] = useState("资产");
    // const [SelectTypeData, setSelectTypeData] = useState<string>('');
    const [SelectName, setSelectName] = useState("");
    const [superiorProject, setSuperiorProject] = useState("");
    const [KMBHAdd, setKMBHAdd] = useState(0);
    const [KMBH, setKMBH] = useState("");
    const [KMBHValue, setKMBHValue] = useState("");
    const [PKMBH, setPKMBH] = useState("");
    const [Satatejc, setSatatejc] = useState("");
    const [KMMC, setKMMC] = useState("");
    const [PKMBHState, setPKMBHState] = useState(false);
    const initialization = () => {
        setVisible(false);
        setKMBHAdd(0);
        setKMBH("");
        setPKMBH("");
        setKMMC("");
        setSatatejc("");
        setSuperiorProject("");
        setSelectedYears("");
        // setSubjectNumber('');
        setKMBHValue("");
        setPKMBHState(false);
        PTextBoxValue.current = "";
    };
    // 自定义方法
    useImperativeHandle(ref, () => ({
        show: (params) => {
            diaParams.current = params;
            setVisible(true);
            setSelectName(params.args.datatKMFX);
            setSelectType(params.args.dataKMLX);
            setKMBHAdd(diaParams.current?.args.ParentLevelKmbh);
            setPKMBH(diaParams.current?.args.PKMBH);
            setSuperiorProject(diaParams.current?.args.ParentLevel);
            setKMBH(diaParams.current?.args.KMBH);
            if (diaParams.current?.args.isEdit) {
                setSelectedYears(diaParams.current?.args.itemNames);
                PTextBoxValue.current = diaParams.current?.args.KMMC;
                setKMMC(diaParams.current?.args.KMMC);
            }
            else {
                PTextBoxValue.current = "";
                setSelectedYears("");
            }
            setKMMC("");
        },
        close: () => {
            setVisible(false);
        },
    }));
    const changeObjData = useCallback(() => {
        const SubjectNumber = PTextBoxValue.current;
        let tempObj = {};
        const reg = /^[\s\S]*.*[^\s][\s\S]*$/;
        if (reg.test(SubjectNumber)) {
            if (diaParams.current?.args.isEdit) {
                tempObj = {
                    SubjectNumber,
                    SelectName,
                    SelectType,
                    selectedYears,
                    KMBHAdd,
                };
            }
            else {
                tempObj = {
                    KMBHValue,
                    PKMBHState,
                    KMMC,
                    Satatejc,
                    PKMBH,
                    KMBH,
                    superiorProject,
                    KMBHAdd,
                    SubjectNumber,
                    SelectName,
                    SelectType,
                    selectedYears,
                };
            }
            console.log(tempObj, "SubjectNumber-----@@@");
        }
        else {
            notify("不可输入空白", "warning", 2000);
        }
        return tempObj;
    }, [
        KMBHValue,
        PKMBHState,
        KMMC,
        Satatejc,
        PKMBH,
        KMBH,
        superiorProject,
        KMBHAdd,
        SelectName,
        SelectType,
        selectedYears,
    ]);
    const methods = () => {
        const tempObjData = changeObjData();
        if (!tempObjData.KMBHAdd)
            return notify("请输入科目编号", "warning", 2000);
        diaParams.current?.okCallback(tempObjData);
        initialization();
    };
    const subjectPrompt = () => {
        const tempObjData = changeObjData();
        if (!tempObjData.KMBHAdd)
            return notify("请输入科目编号", "warning", 2000);
        PopupComponentsRef.current.show({
            title: "选择报表项目",
            content: "一级科目未设置对应报表项目，是否继续？",
            okCallback: () => {
                diaParams.current?.okCallback(tempObjData);
                initialization();
            },
        });
    };
    const repeatMethod = async () => {
        const SubjectNumber = PTextBoxValue.current;
        let tempKMMCName = "";
        // 拼接科目编号
        if (!diaParams.current?.args.isEdit) {
            const r = await getSubjectInfo({
                dbname: dbname,
                Data_ID: AccountDataID,
                YSKMBH: KMBHAdd?.toString(),
            });
            if (r.data.length > 0)
                return notify("科目编号重复", "warning", 2000);
        }
        if (superiorProject) {
            if (KMMC) {
                tempKMMCName = KMMC + "-" + SubjectNumber;
            }
            else {
                tempKMMCName = diaParams.current?.args.resolvess + "-" + SubjectNumber;
            }
        }
        else {
            tempKMMCName = SubjectNumber;
        }
        const res = await getSubjectInfo({
            dbname: dbname,
            Data_ID: AccountDataID,
            // superiorProject ? 上级科目有拼接  : 直接负值value
            KMMC: tempKMMCName,
        });
        // debugger;
        if (res.data.length) {
            const tempData = res.data.filter((item) => item.KMBH !== KMBH);
            // debugger;
            if (tempData.length) {
                return notify("科目名称重复", "warning", 2000);
            }
        }
        if (!selectedYears && !superiorProject)
            return subjectPrompt();
        return methods();
    };
    const onCancel = useCallback(() => {
        initialization();
    }, []);
    const onOk = () => {
        if (diaParams.current?.okCallback) {
            if (!PTextBoxValue.current)
                return notify("请输入科目名称", "warning", 2000);
            if (!KMBHAdd)
                return notify("请输入科目编号", "warning", 2000);
            if (!diaParams.current?.args.isEdit) {
                const PKMBH1 = PKMBH.replace(/~\|/g, "");
                const a1 = KMBHAdd
                    ? `${KMBHAdd}`.substring(0, PKMBH1.length)
                    : KMBH.substring(0, PKMBH1.length);
                if (superiorProject && PKMBH1 !== a1)
                    return notify("输入科目编号不符合规则,应该以父级编号为前缀!", "error", 3000);
            }
            repeatMethod();
        }
    };
    // 科目方向
    const onSelectionChangedDirection = (e) => {
        setSelectName(e.selectedItem.name);
    };
    // 科目类型
    const onSelectionChangedType = (e) => {
        setSelectType(e.selectedItem.type);
    };
    const items = [
        {
            id: "0",
            name: "借",
        },
        {
            id: "1",
            name: "贷",
        },
    ];
    return (_jsxs(_Fragment, { children: [_jsx(Popup, { ref: ref, visible: visible, title: diaParams.current?.args?.title, width: 630, height: 470, ...props, onHiding: () => onCancel(), toolbarItems: [
                    {
                        widget: "dxButton",
                        toolbar: "bottom",
                        location: "after",
                        options: {
                            text: "取消",
                            onClick: () => onCancel(),
                        },
                    },
                    {
                        widget: "dxButton",
                        toolbar: "bottom",
                        location: "after",
                        options: {
                            text: "确定",
                            type: "default",
                            onClick: () => onOk(),
                        },
                    },
                ], 
                // dragOutsideBoundary={true}
                resizeEnabled: true, children: _jsxs("div", { style: { width: "100%" }, children: [_jsxs(Form, { colCount: 2, 
                            // labelLocation={"left"}
                            // labelMode={"outside"}
                            focusStateEnabled: true, showColonAfterLabel: true, alignItemLabels: true, className: "SubjectsForm", width: "100%", children: [_jsxs(SimpleItem, { colSpan: 2, label: { text: "上级科目" }, children: [_jsx(TextBox, { readOnly: true, disabled: diaParams.current?.args.isEdit, className: "TextBox_Style", valueChangeEvent: "keyup", value: superiorProject }), _jsxs("span", { style: { position: "absolute", right: "3px", top: "6px" }, children: [_jsx(CpasIcon, { type: "icon-xinzengkemu", onClick: () => {
                                                        if (diaParams.current?.args.isEdit)
                                                            return;
                                                        SubjectRef.current.show({
                                                            title: "选择科目",
                                                            dbname: dbname,
                                                            Data_ID: AccountDataID,
                                                            isHasFZZ: false,
                                                            editUser: username,
                                                            // 选择 模式 0：单选 1：多选连机选择
                                                            selectMode: "0",
                                                            // 是否为辅助账
                                                            auxiliaryState: false,
                                                            // 是否可以选择上级科目
                                                            isSelectSuperior: true,
                                                            okCallback: (r, v) => {
                                                                // 科目审定明细 界面增加一级科目在业务组件 需做更改
                                                                console.log(r, v, "r-------sssssssss----------");
                                                                setKMBHValue(v[0].YSKMBH);
                                                                setKMBHAdd(Number(v[0].YSKMBH));
                                                                setPKMBHState(false);
                                                                setKMBH(v[0].KMBH);
                                                                // 获取选中的数据
                                                                if (Object.prototype.toString.call(r) ===
                                                                    "[object Object]") {
                                                                    setSuperiorProject(r.code + "-" + r.KMMC);
                                                                    console.log(r, "@@@");
                                                                    setPKMBH(r.KMBH);
                                                                    setKMMC(r.KMMC);
                                                                    setSatatejc(r.JC);
                                                                }
                                                            },
                                                        });
                                                    } }), _jsx(CpasIcon, { type: "icon-close1", onClick: () => {
                                                        if (diaParams.current?.args.isEdit)
                                                            return;
                                                        setSuperiorProject("");
                                                        setPKMBH("");
                                                        setSatatejc("");
                                                        if (KMBHAdd) {
                                                            const value2 = `${KMBHAdd}${"~|"}`;
                                                            setKMBH(value2);
                                                        }
                                                        else {
                                                            setKMBH("");
                                                        }
                                                        setPKMBHState(true);
                                                    } })] })] }), _jsx(SimpleItem, { colSpan: 2, label: { text: "科目编号" }, children: _jsx(NumberBox, { disabled: diaParams.current?.args.isEdit, value: KMBHAdd == 0 ? undefined : KMBHAdd, onChange: (e) => {
                                            const { event } = e;
                                            setKMBHAdd(Number(event.target.value));
                                            const PKMBH1 = PKMBH.replace(/~\|/g, "");
                                            const value1 = event.target.value.slice(PKMBH1.length);
                                            const value2 = `${PKMBH}${value1}${"~|"}`;
                                            console.log(value2, "value2----");
                                            setKMBH(value2);
                                            setKMBHValue(event.target.value);
                                        } }) }), _jsx(SimpleItem, { colSpan: 2, label: { text: "科目名称" }, children: _jsx(PTextBox, { ref: pTextBoxRef, defaultValue: PTextBoxValue.current, changeData: (data) => {
                                            PTextBoxValue.current = data;
                                        } }) }), _jsx(SimpleItem, { colSpan: 2, label: { text: "科目方向" }, children: _jsx(SelectBox, { dataSource: items, value: SelectName ? SelectName : items[0].name, displayExpr: "name", valueExpr: "name", onSelectionChanged: onSelectionChangedDirection }) }), _jsx(SimpleItem, { colSpan: 2, label: { text: "科目类型" }, children: _jsx(SelectBox, { dataSource: diaParams.current?.args.data, value: SelectType, 
                                        // defaultValue="资产"
                                        displayExpr: "type", valueExpr: "type", onSelectionChanged: onSelectionChangedType }) }), _jsx(SimpleItem, { colSpan: 2, children: _jsx("div", { style: {
                                            width: "100%",
                                            borderBottom: "1px solid rgb(0,0,0,0.10)",
                                            height: "10px",
                                        } }) }), _jsxs(SimpleItem, { colSpan: 2, label: { text: "对应报表项目" }, children: [_jsx(TextBox, { value: selectedYears, readOnly: true, className: "TextBox_Style", valueChangeEvent: "keyup", disabled: true, placeholder: "请选择....." }), _jsxs("span", { style: { position: "absolute", right: "6px", top: "6px" }, children: [_jsx(CpasIcon, { type: "icon-xinzengkemu", onClick: () => {
                                                        statementProjectRef.current.show({
                                                            title: "选择报表项目",
                                                            dbname: dbname,
                                                            Data_ID: AccountDataID,
                                                            ownerReport: "",
                                                            selectMode: "1",
                                                            // 选择 模式 0：单选 1：多选连机选择
                                                            okCallback: (r) => {
                                                                console.log(r, "@@@");
                                                                const res = r
                                                                    .map((item) => {
                                                                    return item.itemName;
                                                                })
                                                                    .join();
                                                                setSelectedYears(res);
                                                            },
                                                        });
                                                    } }), _jsx(CpasIcon, { type: "icon-close1", onClick: () => {
                                                        setSelectedYears("");
                                                    } })] })] })] }), _jsx("div", { style: {
                                width: "100%",
                                display: "flex",
                                alignItems: "center",
                                justifyContent: "end",
                                marginTop: "10px",
                            }, children: _jsx("span", { style: { fontSize: "12px", color: "#666" }, children: "\u4E00\u7EA7\u6216\u5907\u62B5\u79D1\u76EE\u9700\u6307\u5B9A\u5BF9\u5E94\u62A5\u8868\u9879\u76EE" }) }), _jsx("div", { style: {
                                width: "100%",
                                height: "14px",
                                borderBottom: "1px solid rgb(0,0,0,0.10)",
                            } })] }) }), _jsx(AddPopup, { ref: SubjectRef }), _jsx(ReportPopup, { ref: statementProjectRef }), _jsx(PopupComponents, { ref: PopupComponentsRef })] }));
});
export default React.memo(SubjectPopupData);
